
/***************************************************************************
 *   confab 
 *                                                                         *
 *   Copyright (C) 2010 by kuerant                                         *
 *                         <kuerant@gmail.com>                             *
 *                                                                         *
 *   This library is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU Library General Public License as       *
 *   published by the Free Software Foundation; either version 2 of the    *
 *   License, or (at your option) any later version.                       *
 *                                                                         *
 *   This library is distributed in the hope that it will be usefull, but  *
 *   WITHOUT ANY WARRANTY; without even the implied warranty of            *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      *
 *   Library General Public License for more details.                      *
 *                                                                         *
 *   You should have received a copy of the GNU Library General Public     *
 *   License along with this library; if not, write to the Free Software   *
 *   Foundation, Inc., 59 Temple Place - Suite 330, Boston,                *
 *   MA 02111-1307, USA                                                    *
 ***************************************************************************/

#include <cstdio>

#include <cstdlib>

#include <boost/filesystem.hpp>

#include "bocat/common/ini.hpp"

#include "store.hpp"

#include "cstore.hpp"


namespace	confab	{

#define	N_CHECKPOINT_INTERVAL	10000

///////////////////////////////////////////////////////////////////////
//  class SleepyStore
//

SleepyStore::SleepyStore( const char* config_filename )
			:	_config( config_filename ), 
				_aucab( _config.envhome, _config.dbhome, _config.datahome )
				
{

	// check env-home directory
	boost::filesystem::path	envhome_path( _config.envhome );
	if ( !boost::filesystem::exists( envhome_path ) ) {
		char	msg[512];
		snprintf(msg, sizeof(msg) - 1, "Env home '%s' does NOT exist", _config.envhome.c_str());
		throw std::runtime_error( msg );
	}
	if ( !boost::filesystem::is_directory( envhome_path ) ) {
		char	msg[512];
		snprintf(msg, sizeof(msg) - 1, "Env home '%s' is NOT a directory", _config.envhome.c_str());
		throw std::runtime_error( msg );
	}

	// check db-home directory
	boost::filesystem::path	dbhome_path( _config.dbhome );
	if ( !boost::filesystem::exists( dbhome_path ) ) {
		char	msg[512];
		snprintf(msg, sizeof(msg) - 1, "DB home '%s' does NOT exist", _config.dbhome.c_str());
		throw std::runtime_error( msg );
	}
	if ( !boost::filesystem::is_directory( dbhome_path ) ) {
		char	msg[512];
		snprintf(msg, sizeof(msg) - 1, "DB home '%s' is NOT a directory", _config.dbhome.c_str());
		throw std::runtime_error( msg );
	}

	// check data-home directory
	boost::filesystem::path	datahome_path( _config.datahome );
	if ( !boost::filesystem::exists( datahome_path ) ) {
		char	msg[512];
		snprintf(msg, sizeof(msg) - 1, "Data home '%s' does NOT exist", _config.datahome.c_str());
		throw std::runtime_error( msg );
	}
	if ( !boost::filesystem::is_directory( datahome_path ) ) {
		char	msg[512];
		snprintf(msg, sizeof(msg) - 1, "Data home '%s' is NOT a directory", _config.datahome.c_str());
		throw std::runtime_error( msg );
	}
}

SleepyStore::~SleepyStore() 
{
} 


///////////////////////////////////////////////////////////////////////
//


int     
SleepyStore::insert( const std::string& kpin,
                     const std::string& key, 
                     const std::string& data )
{
    int cid = -1;

    return  _aucab.insert( cid, kpin, key, data );
}


int     
SleepyStore::find( const std::string& kpin,
                   const std::string& key, 
                         std::string& buf  )
{
    int cid = -1;

    return  _aucab.find( cid, kpin, key, buf );
}


int     
SleepyStore::erase( const std::string& kpin,
                    const std::string& key )
{
    int cid = -1;

    return  _aucab.erase( cid, kpin, key );
}




///////////////////////////////////////////////////////////////////////
//

SleepyConfig::SleepyConfig( const char* config_filename )
{
	this->parseConfig( config_filename );
}


int
SleepyConfig::parseConfig( const char* config_filename )
{
	/*
 	 *       INI config
	 *   [confab::store]
	 *   envhome = /path/to/env/home
	 *   dbhome  = /path/to/db/home
	 *   datahome  = /path/to/data/home
	 *
	 */

	bocat::common::INIFile	ini( config_filename );
	
	std::string	envhome = ini[ INI_SECTION ][ "envhome" ];
	if (envhome.size() < 1) {
		char	msg[512];
		snprintf(msg, sizeof(msg) - 1, "No 'envhome' or [%s] in config file %s", INI_SECTION, config_filename);
		throw std::runtime_error( msg );
	}

	std::string	dbhome = ini[ INI_SECTION ][ "dbhome" ];
	if (dbhome.size() < 1) {
		char	msg[512];
		snprintf(msg, sizeof(msg) - 1, "No 'dbhome' or [%s] in config file %s", INI_SECTION, config_filename);
		throw std::runtime_error( msg );
	}

	std::string	datahome = ini[ INI_SECTION ][ "datahome" ];
	if (datahome.size() < 1) {
		char	msg[512];
		snprintf(msg, sizeof(msg) - 1, "No 'datahome' or [%s] in config file %s", INI_SECTION, config_filename);
		throw std::runtime_error( msg );
	}

    this->envhome = envhome;
    this->dbhome = dbhome;
    this->datahome = datahome;

	return 0;
}

}	//// namespace confab

